package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.common.UserHolder;
import com.itheima.ydd.entity.*;
import com.itheima.ydd.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author yan.zhou
 * @date 2022/2/16
 */
@RestController
@RequestMapping("sys_config")
public class ConfigController {
    /**
     * update   POST    已测
     * all      GET     已测
     */
    @Autowired
    private ConfigService configService;
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private RoleMenuService roleMenuService;
    @Autowired
    private MenuService menuService;

    @GetMapping("all")
    public ResultData all() {

        Map<String, Object> map = configService.all();
        return ResultData.ok("", map);
    }
    //增加配置
    @PostMapping("update")
    public ResultData update(@RequestBody List<Config> configs){
        LambdaQueryWrapper<Role> roleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //获取登录人员信息，把test类型转化为string，去掉[]和""
        String userId = UserHolder.getUserId();
        User user2 = userService.getById(userId);
        String strip = StringUtils.strip(user2.getRole_id(), "[]");
        String[] split = strip.split(",");
        for (int i = 0; i < split.length; i++) {
            split[i]=StringUtils.replace(split[i], "\"", "");
        }
        //String replace = StringUtils.replace(strip, "\"", "");
        //String[] split = replace.split(",");
        roleLambdaQueryWrapper.in(Role::getId,split);
        List<Role> list1 = roleService.list(roleLambdaQueryWrapper);
        List<String> collect = list1.stream().map((item) -> {
            return item.getId();
        }).collect(Collectors.toList());
        //System.out.println(collect);
        //根据权限id去查询是否有用户管理权限
        LambdaQueryWrapper<RoleMenu> roleMenuLambdaQueryWrapper=new LambdaQueryWrapper<>();
        roleMenuLambdaQueryWrapper.in(RoleMenu::getRoleId,collect);
        //roleMenuLambdaQueryWrapper.in(RoleMenu::getRoleId,collect);
        List<RoleMenu> list = roleMenuService.list(roleMenuLambdaQueryWrapper);
        for (RoleMenu roleMenu : list) {
            String menuId = roleMenu.getMenuId();
            Menu menu = menuService.getById(menuId);
            //一级权限
            if ("系统设置".equals(menu.getTitle())) {
                LambdaQueryWrapper<Menu> menuLambdaQueryWrapper1 = new LambdaQueryWrapper<>();
                menuLambdaQueryWrapper1.eq(Menu::getPid, menuId);
                List<Menu> list3 = menuService.list(menuLambdaQueryWrapper1);
                for (Menu Menu2 : list3) {
                    //二级权限
                    if ("系统配置".equals(Menu2.getTitle())) {
                        LambdaQueryWrapper<Menu> menuLambdaQueryWrapper = new LambdaQueryWrapper<>();
                        menuLambdaQueryWrapper.eq(Menu::getPid, Menu2.getId());
                        List<Menu> list2 = menuService.list(menuLambdaQueryWrapper);
                        for (Menu Menu1 : list2) {
                            //Menu menu1 = menuService.getById(Menu1.getId());
                            //三级权限
                            if ("保存".equals(Menu1.getTitle())) {
                                //更新数据
                                for (Config config : configs) {
                                    configService.updateById(config);
                                }
                                return ResultData.ok("更新成功", "更新成功");
                            }
                        }
                    }
                }
            }
        }
        //否则没有权限添加
        return ResultData.error("更新失败,无权限更新!");
    }
}